package com.txw.javaCore.server.controller;

import com.txw.javaCore.api.response.BaseResponse;
import com.txw.javaCore.api.response.StatusCode;
import com.txw.javaCore.server.service.QrCodeService;
import com.txw.javaCore.server.util.QRCodeUtil;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @PROJECT_NAME: JavaCoreB2
 * @DESCRIPTION:
 * @Author: 涂玄武
 * @DATE: 2020/12/23 17:01
 */
@RestController
@RequestMapping(value = "qr/code")
public class QrCodeController extends BaseController {

    @Autowired
    private Environment env;

    private static final ThreadLocal<SimpleDateFormat> LOCALDATEFORMAT = ThreadLocal.withInitial(() -> new SimpleDateFormat(
            "yyyyMMddHHmmss"
    ));

    /**
     * 生成二维码并将其存放于文件目录
     * google zxing 开发工具包
     *
     * @param content
     * @return
     */
    @PostMapping(value = "generate/v1")
    public BaseResponse generateV1(@RequestParam String content) {
        if (StringUtils.isBlank(content)) {
            return new BaseResponse(StatusCode.InvalidParams);
        }
        log.info("--前端接受的请求参数：{}", content);
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            final String fileName = LOCALDATEFORMAT.get().format(DateTime.now().toDate());
            QRCodeUtil.createCodeToFile(content, new File(env.getProperty("qrcode.root.path")), fileName + env.getProperty("qrcode.suffix"));
            LOCALDATEFORMAT.remove();
        } catch (Exception e) {
            log.error("生成二维码-存放于文件目录发生异常：{}", e.getMessage());
            response = new BaseResponse(StatusCode.Fail.getCode(), e.getMessage());
        }
        return response;
    }

    /**
     * 生成二维码图片流并将其返回给前端调用者
     * google zxing 开发工具包
     *
     * @param content
     * @return
     */
    @PostMapping(value = "generate/v2")
    public BaseResponse generateV2(@RequestParam String content, HttpServletResponse httpServletResponse) {
        if (StringUtils.isBlank(content)) {
            return new BaseResponse(StatusCode.InvalidParams);
        }
        log.info("--前端接受的请求参数：{}", content);
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            QRCodeUtil.createCodeToOutputStream(content, httpServletResponse.getOutputStream());
        } catch (Exception e) {
            log.error("生成二维码-存放于文件目录发生异常：{}", e.getMessage());
            response = new BaseResponse(StatusCode.Fail.getCode(), e.getMessage());
        }
        return response;
    }

    @Autowired
    private QrCodeService qrCodeService;

    /**
     * 生成二维码图片并将其返回给前端调用者_hutool
     *
     * @param content
     * @return
     */
    @PostMapping(value = "generate/v3")
    public BaseResponse generateV3(@RequestParam String content, HttpServletResponse httpServletResponse) {
        if (StringUtils.isBlank(content)) {
            return new BaseResponse(StatusCode.InvalidParams);
        }
        log.info("--前端接受的请求参数：{}", content);
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
//            final String filePath = env.getProperty("qrcode.root.path") + File.separator + LOCALDATEFORMAT.get().format(DateTime.now().toDate()) + env.getProperty("qrcode.suffix");
//            //第一种形式：生成二维码图片
//            qrCodeService.createCodeToFile(content, filePath);
//            //线程副本资源用完移除，避免资源浪费
//            LOCALDATEFORMAT.remove();
            //第二种形式：将生成的二维码图片以流的形式输出到调用端
            qrCodeService.createCodeToStream(content,httpServletResponse);
        } catch (Exception e) {
            log.error("生成二维码-存放于文件目录发生异常：{}", e.getMessage());
            response = new BaseResponse(StatusCode.Fail.getCode(), e.getMessage());
        }
        return response;
    }


}
